package main

import (
	"fmt"
	"sort"
)

//#link:https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/
func main() {
	s := "aewfafwafjlwajflwajflwafj"
	dictionary := []string{"ewaf"}

	fmt.Println(findLongestWord(s, dictionary))
}

func findLongestWord(s string, dictionary []string) string {
	//1.先把字典进行排序
	sort.Slice(dictionary, func(i, j int) bool {
		if len(dictionary[i]) == len(dictionary[j]) {
			return dictionary[i] < dictionary[j]
		}
		return len(dictionary[i]) > len(dictionary[j])
	})
	//2.遍历字典 并判断是否存在
	for _, dic := range dictionary {
		i := 0
		for j := 0; j < len(s); j++ {
			if s[j] == dic[i] {
				i++
			}
			if i == len(dic) {
				return dic
			}
		}
	}
	return ""
}

func join(a, b []byte) bool {
	//a是原字符串
	tmpMap := make(map[byte][]int)
	for index, aValue := range a {
		tmpMap[aValue] = append(tmpMap[aValue], index)
	}
	//初始化第一个索引位
	beforIndex := 0
	for _, bValue := range b {
		if v, ok := tmpMap[bValue]; ok {
			if len(v) > 0 {
				firstIndex := v[0]
				if beforIndex <= firstIndex {
					v = v[1:]
					if len(v) == 0 {
						delete(tmpMap, bValue)
					}
					beforIndex = firstIndex
				} else {
					return false
				}
			} else {
				return false
			}
		} else {
			return false
		}
	}
	return true
}
